using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace FBPChaos
{
	public class Wizard
	{
        const int numberOfSpells = 10;
        List<Spell> spells;
		Point position;
		int movement;
		int range;
		bool ranged;
		int rangedDamage;
		bool magicSword;
		bool magicBow;
		bool magicArmour;
		bool magicShield;
		int offense;
		int defense;
		int magicResistance;
        bool alive;
        string image;
        Color colour;

        public Wizard(Point newPosition, List<Spell> availableSpells)
        {
            magicSword = false;
            magicBow = false;
            magicArmour = false;
            magicShield = false;
            ranged = false;
            alive = true;
            image = "default_wizard";
            Random random = new Random();
            movement = 1;
            offense = random.Next(7);
            defense = random.Next(7);
            magicResistance = random.Next(7);
            position = newPosition;
            colour = Color.White;
            spells = new List<Spell>(numberOfSpells);
            generateSpells(availableSpells);
        }

		public Point Position
		{
			get
			{
                return position;
			}
			set
			{
                position = value;
			}
		}

        public bool Alive
        {
            get { return alive; }
            set { alive = value; }
        }

        public int Defense
        {
            get { return defense; }
        }

		public void moveWizard()
		{
			throw new NotImplementedException();
		}

		public void killWizard()
		{
			alive = false;
		}

        public string Image
        {
            get { return image; }

        }

        public Color Colour
        {
            get { return colour; }
            set { colour = value; }
        }

        public string[] listSpells()
        {
            string[] list = new string[spells.Count];
            for (int counter = 0; counter < spells.Count; counter++)
            {
                list[counter] = spells[counter].Name;

            }
            return list;
        }

        public int NumberOfSpells
        {
            get { return spells.Count; }
        }

        private void generateSpells(List<Spell> availableSpells)
        {
            int total = 0;
            Random random = new Random();
            int randomNumber;
            int lowerLimit = 0;
            int whileCounter;

            //add disbelieve spell
            whileCounter = 0;
            while (availableSpells[whileCounter].Name != "Disbelieve")
            {
                whileCounter++;
            }
            spells.Add(availableSpells[whileCounter]);

            //establish total probability 
            for (int counter = 0; counter < availableSpells.Count; counter++)
            {
                if (availableSpells[counter].Name != "Disbelieve")
                {
                    total = total + availableSpells[counter].Chance;
                }
            }

            Console.WriteLine(total.ToString());

            for (int counter = 0; counter < numberOfSpells; counter++)
            {
                randomNumber = random.Next(total);
                Console.Write("Random = " + randomNumber.ToString());
                whileCounter = -1;
                do
                {
                    whileCounter++;
                    if (availableSpells[whileCounter].Name != "Disbelieve")
                    {
                        //count until spell found
                        lowerLimit = lowerLimit + availableSpells[whileCounter].Chance;
                        //e.g. random number = 26
                        //Chances are 10 10 5 10
                        //Lower Limit 10 20 25 35
                        Console.WriteLine("Chance is at; " + lowerLimit.ToString());
                        //Selects spell 4 (i.e. index 3)
                    }
                    
                } while (lowerLimit <= randomNumber);
                spells.Add(availableSpells[whileCounter]);
                lowerLimit = 0;
                Console.WriteLine("Spell: " + availableSpells[whileCounter].Name + " added to list");
            }
        }
	}
}
